/*
	添加管理员
*/

var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

//dom
$(function(){
	layui.use(['form', 'layer'], function(){
		var form = layui.form, layer = layui.layer;
		var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };

		function addNewAdmin(adminData){
			/*
				经销商新增算法
				@agentData 新增的经销商数据
			*/
			loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var  _ajaxData = $.extend({}, adminData, ajaxData);
			$.ajax({
				url: BASE_SERVICEURL + URlS.admin,
				type: 'PUT',
				data: _ajaxData,
				success: function(responseData) {
					layer.close(loadIndex);
					console.log(responseData)
					if(responseData.msg.code == 200){
						layer.msg(responseData.msg.msg, {time: 2000, end: function(){
							parent.location.reload();
						}})
					}else{
						layer.msg(responseData.msg.msg, {time: 2000});
					};
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						window.location.href = './500.html';
					}});
				}
			})
		}
		form.on('submit(add)', function(data){
			/*
				提交新增表单
				@ data layui form 数据
			*/
			layer.confirm(MESSAGES.confirm_add, {title: '新增提示'}, function(index){
				var f = data.field;
				//console.log(f)
				var md = {
					aName: f.username,
					aPhone: f.phone,
					aPwd: hex_md5(f.pass),
					aRole: f.role,
					aMail: f.email
				};
				addNewAdmin(md);
				layer.close(index);
			});
			return false;
		});
	})
})

















